package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.TablePCapitalEquiment;
import com.ruoyi.system.domain.TablePProjectTask;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITablePCapitalEquimentService;
import com.ruoyi.system.service.ITablePProjectTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TablePProject;
import com.ruoyi.system.service.ITablePProjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 * 
 * @author ruoyi
 * @date 2022-05-17
 */
@Controller
@RequestMapping("/system/project")
public class TablePProjectController extends BaseController
{
    private String prefix = "system/project";

    @Autowired
    private ITablePProjectService tablePProjectService;

    @Autowired
    private ITablePProjectTaskService tablePProjectTaskService;

    @Autowired
    private ITablePCapitalEquimentService tablePCapitalEquimentService;

    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("system:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询项目信息列表
     */
    @RequiresPermissions("system:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TablePProject tablePProject)
    {
        startPage();
        List<TablePProject> list = tablePProjectService.selectTablePProjectList(tablePProject);
        for(TablePProject pProject:list){
            TablePProjectTask tablePProjectTask = new TablePProjectTask();
            tablePProjectTask.setApproveStatus(1l);
            tablePProjectTask.setProjectId(pProject.getId());
            Integer total = 0;
            Long equipmentAmountTotal = 0l;
            List<TablePProjectTask> pProjectTasks = tablePProjectTaskService.selectTablePProjectTaskList(tablePProjectTask);
            if(pProjectTasks != null && pProjectTasks.size()>0){
                for(int i=0;i<pProjectTasks.size();i++){
                    //获取设备资金信息
                    TablePCapitalEquiment  tablePCapitalEquiment = new TablePCapitalEquiment();
                    tablePCapitalEquiment.setTaskId(pProjectTasks.get(i).getId());
                    List<TablePCapitalEquiment> tablePCapitalEquimentList = tablePCapitalEquimentService.selectTablePCapitalEquimentList(tablePCapitalEquiment);
                    if(tablePCapitalEquimentList != null && tablePCapitalEquimentList.size()>0){
                        for(int j=0;j<tablePCapitalEquimentList.size();j++){
                            equipmentAmountTotal = equipmentAmountTotal+tablePCapitalEquimentList.get(j).getEquipment();
                            //bigDecimal.add(tablePCapitalEquimentList.get(j).getCapital());
                            total = total+tablePCapitalEquimentList.get(j).getCapital();
                        }
                    }

                }
            }

            pProject.setCapitalTotal(total);
            pProject.setEquipmentAmountTotal(equipmentAmountTotal);

        }
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @RequiresPermissions("system:project:export")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TablePProject tablePProject)
    {
        List<TablePProject> list = tablePProjectService.selectTablePProjectList(tablePProject);
        ExcelUtil<TablePProject> util = new ExcelUtil<TablePProject>(TablePProject.class);
        return util.exportExcel(list, "项目信息数据");
    }

    /**
     * 新增项目信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目信息
     */
    @RequiresPermissions("system:project:add")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TablePProject tablePProject)
    {
        return toAjax(tablePProjectService.insertTablePProject(tablePProject));
    }

    /**
     * 修改项目信息
     */
    @RequiresPermissions("system:project:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TablePProject tablePProject = tablePProjectService.selectTablePProjectById(id);
        mmap.put("tablePProject", tablePProject);

        //获取用户信息
        mmap.put("users",userService.selectUserList(new SysUser()));
        return prefix + "/edit";
    }

    /**
     * 修改保存项目信息
     */
    @RequiresPermissions("system:project:edit")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TablePProject tablePProject)
    {
        return toAjax(tablePProjectService.updateTablePProject(tablePProject));
    }

    /**
     * 删除项目信息
     */
    @RequiresPermissions("system:project:remove")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tablePProjectService.deleteTablePProjectByIds(ids));
    }
}
